/**
 * Created by cjd on 2016/6/9.
 */
angular.module("app").controller("org.info.Ctrl", ["$scope", "$http", "org", function ($scope, $http, org) {
    $scope.viewOrginfo = function(data){
        $('select').attr('disabled',true)
        var responseData = JSON.parse(sessionStorage.getItem("areainfo")), $province = $('#provinceData'), $city = $('#cityData'), $area = $('#areaData'), $street = $('#streetData'), $committee = $('#committeeData'), jurisdiction = data.jurisdiction
        var userProvinceId = jurisdiction.substring(0,2)+'0000', userCityId = jurisdiction.substring(0,4)+'00', userAreaId = jurisdiction.substring(0,6), userStreetId = jurisdiction.substring(0,9)+'000', userCommitteeId = jurisdiction.substring(0,12)
        if(jurisdiction.length > 6){
            $('.street-box').show()
        }else{
            $('.street-box').hide()
        }
        $.each(responseData.data.regionList, function(index,val){
            if(val.levelType == 1){
                if(val.id == userProvinceId){
                    $("<option value="+val.id+" data-parentId="+val.parentId+" selected>"+val.name+"</option>").appendTo($province)
                }
            }
            if(val.parentId == userProvinceId && val.levelType == 2) {
                if(val.id == userCityId){
                    $("<option value=" + val.id + " data-parentId=" + val.parentId + " selected>" + val.name + "</option>").appendTo($city)
                }
            }
            if(val.parentId == userCityId && val.levelType == 3) {
                if(val.id == userAreaId){
                    $("<option value=" + val.id + " data-parentId=" + val.parentId + " selected>" + val.name + "</option>").appendTo($area)
                }
            }
        })
        $.each(responseData.data.countyList, function(index,val){
            if(val.parentId == userAreaId && val.countrysideType == 0) {
                if(val.code == userStreetId){
                    $("<option value=" + val.code + " data-parentId=" + val.parentId + " selected>" + val.name + "</option>").appendTo($street)
                }
            }
            if(val.parentId == userStreetId && val.countrysideType != 0) {
                if(val.code == userCommitteeId){
                    $("<option value=" + val.code + " data-parentId=" + val.parentId + " selected>" + val.name + "</option>").appendTo($committee)
                }
            }
        })
    }
    $scope.treeViewConfig = angular.copy($scope.$parent.treeViewConfig);
    $scope.treeViewConfig.onItemClick = function(e){
        $scope.orgInfo = e.itemData;
        $scope.viewOrginfo($scope.orgInfo)
    };
    $scope.orgInfo = $scope.$parent.treeViewDataSource[0];
    $scope.viewOrginfo($scope.orgInfo)
}]);